﻿--[[	$Id: watchFrame.lua 3726 2013-11-15 12:25:16Z sdkyron@gmail.com $	]]

local _, caelUI = ...

caelUI.watchframe = caelUI.createModule("WatchFrame")

watchframe = caelUI.watchframe

local dummy, pixelScale = caelUI.dummy, caelUI.scale

watchframe:SetWidth(pixelScale(223))
watchframe:SetHeight(pixelScale(caelUI.screenHeight / 2))
watchframe:SetPoint("TOPRIGHT", UIParent, pixelScale(-5), pixelScale(-5))
watchframe:EnableMouse(false)

WatchFrame:ClearAllPoints()
WatchFrame:SetPoint("TOP", watchframe)
WatchFrame:SetHeight(pixelScale(caelUI.screenHeight / 2))

hooksecurefunc(WatchFrame, "SetPoint", function(_, _, parent, point)
	if parent ~= watchframe then
		WatchFrame:ClearAllPoints()
		WatchFrame:SetPoint("TOP", watchframe)
	end
end)

WatchFrameTitle:SetFont(caelMedia.fonts.ADDON_FONT, 11)

WatchFrameCollapseExpandButton:SetNormalTexture([[Interface\Addons\caelUI\media\miscellaneous\watchframeCollapse]])
WatchFrameCollapseExpandButton:SetPushedTexture([[Interface\Addons\caelUI\media\miscellaneous\watchframeCollapse]])

local index = 1
local itemIndex = 1

hooksecurefunc("WatchFrame_Update", function()
	local line = _G["WatchFrameLine"..index]

	while line do
		line.dash:SetFont(caelMedia.fonts.ADDON_FONT, 9)

		line.text:SetFont(caelMedia.fonts.ADDON_FONT, 9)
		line.text:SetSpacing(2)
		line.text:ClearAllPoints()
		line.text:SetPoint("LEFT", line.dash, "RIGHT", pixelScale(-2), pixelScale(2))

		index = index + 1

		line = _G["WatchFrameLine"..index]
	end

	local button = _G["WatchFrameItem"..itemIndex]

	while button do
		local hotkey = _G["WatchFrameItem"..itemIndex.."HotKey"]

		button:SetScale(0.85)

		button.normal = button:CreateTexture("Frame", nil, self)
		button.normal:SetTexture(caelMedia.files.buttonNormal)
		button.normal:SetPoint("TOPLEFT", pixelScale(-3.5), pixelScale(3.5))
		button.normal:SetPoint("BOTTOMRIGHT", pixelScale(3.5), pixelScale(-3.5))
		button.normal:SetVertexColor(0.25, 0.25, 0.25)

		button:SetNormalTexture(button.normal)

		button.gloss = button:CreateTexture("Frame", nil, self)
		button.gloss:SetTexture(caelMedia.files.buttonGloss)
		button.gloss:SetPoint("TOPLEFT", pixelScale(-4.5), pixelScale(4.5))
		button.gloss:SetPoint("BOTTOMRIGHT", pixelScale(4.5), pixelScale(-4.5))
		button.gloss:SetVertexColor(1, 1, 1, 0.5)

		button.pushed = button:CreateTexture("Frame", nil, self)
		button.pushed:SetTexture(caelMedia.files.buttonNormal)
		button.pushed:SetPoint("TOPLEFT", pixelScale(-3.5), pixelScale(3.5))
		button.pushed:SetPoint("BOTTOMRIGHT", pixelScale(3.5), pixelScale(-3.5))
		button.pushed:SetVertexColor(1, 1, 1)

		button:SetPushedTexture(button.pushed)

		button:SetHighlightTexture("")

		_G["WatchFrameItem"..itemIndex.."IconTexture"]:SetTexCoord(0.08, 0.92, 0.08, 0.92)

		hotkey:ClearAllPoints()
		hotkey:SetPoint("TOPRIGHT", pixelScale(7), pixelScale(-1.5))
		hotkey:SetFont(caelMedia.fonts.CUSTOM_NUMBERFONT, 11, "OUTLINEMONOCHROME")
		hotkey:SetJustifyH("CENTER")

		itemIndex = itemIndex + 1

		button = _G["WatchFrameItem"..itemIndex]
	end

	local questIndex
	local numQuestWatches = GetNumQuestWatches()

	for i = 1, numQuestWatches do
		questIndex = GetQuestIndexForWatch(i)
		if questIndex then
			local title, level = GetQuestLogTitle(questIndex)
			local color = GetQuestDifficultyColor(level)

			for j = 1, #WATCHFRAME_QUESTLINES do
				if WATCHFRAME_QUESTLINES[j].text:GetText() == title then
					WATCHFRAME_QUESTLINES[j].text:SetTextColor(color.r, color.g, color.b)
					WATCHFRAME_QUESTLINES[j].color = color
				end
			end
		end
	end
end)

hooksecurefunc("WatchFrameLinkButtonTemplate_Highlight", function(self, onEnter)
	for i = self.startLine, self.lastLine do
		if not self.lines[i] then return end
		if self.lines[i].color then
			if onEnter then
				self.lines[i].text:SetTextColor(1, 1, 0)
			else
				self.lines[i].text:SetTextColor(self.lines[i].color.r, self.lines[i].color.g, self.lines[i].color.b)
			end
		end
	end
end)

local BossExists = function()
	for i = 1, MAX_BOSS_FRAMES do
		if UnitExists("boss"..i) and UnitIsEnemy("player", "boss"..i) then
			return true
		end
	end
end

local ZoneChange = function(zone)
	local _, instanceType = IsInInstance()

	if instanceType == "arena" or BossExists() then
		if not WatchFrame.collapsed then
			WatchFrame_CollapseExpandButton_OnClick(WatchFrame_CollapseExpandButton)
		end
	elseif WatchFrame.collapsed and not InCombatLockdown() then
		WatchFrame_CollapseExpandButton_OnClick(WatchFrame_CollapseExpandButton)
	end
end

watchframe:SetScript("OnEvent", function(self, event)
	local zone = GetRealZoneText()
	if zone and zone ~= "" then
		return ZoneChange(zone)
	end
end)

for _, event in next, {
	"INSTANCE_ENCOUNTER_ENGAGE_UNIT",
	"PLAYER_ENTERING_WORLD",
	"PLAYER_REGEN_ENABLED",
	"UNIT_TARGETABLE_CHANGED",
} do
	watchframe:RegisterEvent(event)
end